cmake_minimum_required(VERSION 2.8)

#set(CMAKE_BUILD_TYPE Release)
#set(CMAKE_BUILD_TYPE Release CACHE STRING "set build type to release")

#message(project_dir=${PROJECT_SOURCE_DIR})

#if (${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
#	message(FATAL_ERROR "Please into another dir to build!")
#endif()


add_definitions("-O2")
set(UNIX_OS true)

include_directories(
	${CMAKE_CURRENT_SOURCE_DIR}/src
	${CMAKE_CURRENT_SOURCE_DIR}/include
	${CMAKE_CURRENT_SOURCE_DIR}/ext
	${CMAKE_CURRENT_SOURCE_DIR}/ext/libdatachannels/src
	${CMAKE_CURRENT_SOURCE_DIR}/ext/libdatachannels/src/internal
	${CMAKE_CURRENT_SOURCE_DIR}/ext/crc32c/include
	${CMAKE_CURRENT_SOURCE_DIR}/ext/crc32c/config/Linux-x86_64
)

set(src ${CMAKE_CURRENT_SOURCE_DIR}/src)

# 添加文件夹下面的全部文件
set(sources
	# ${src}
	${src}/aac
	${src}/flv
	${src}/g711
	${src}/g722
	${src}/gsm
	${src}/h263
	${src}/h264
	# ${src}/nelly
	${src}/opus
	${src}/rtmp
	${src}/rtp
	#${src}/speex
	${src}/vp6
	${src}/vp8
	${src}/vp9
	${src}/ws
)


foreach(iter ${sources})
	aux_source_directory(${iter} lib_src)
endforeach()

# 添加单个文件
set(lib_src ${lib_src} 
	${src}/utf8.cpp
	${src}/avcdescriptor.cpp
)

##############################################################################

if (${UNIX_OS})
	add_definitions(
		"-W"
		"-fPIC"
		"-DPIC"
		"-Wall"
		"-fpermissive"
		# "-Werror"
		# "-Wshadow"
		"-Wpointer-arith"
		"-Waggregate-return"
		"-Wno-long-long"
		"-Wuninitialized"
		"-D_POSIX_PTHREAD_SEMANTICS"
		"-DFLOATING_POINT"
		"-D__SSE2__"
		"-Wno-narrowing"
		"-DOUTSIDE_SPEEX"
		"-std=c++17"
		"-std=gnu++17"
		"-march=native"
		"-DRANDOM_PREFIX=mcu"
		"-msse -msse2 -msse3 -msse4.1"
		"-DHAVE_STD_ALIGNED_ALLOC -DLINUX"
	)

	if (CMAKE_COMPILER_IS_GNUCC)
		execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
			OUTPUT_VARIABLE GCC_VERSION)
		string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
		list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
		# list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)

		if(CMAKE_VERSION GREATER "3.0")
			if ((GCC_MAJOR GREATER_EQUAL 4) AND (GCC_MINOR GREATER_EQUAL 8))
				message(STATUS "gcc version major=${GCC_MAJOR}")
				message(STATUS "gcc version minor=${GCC_MINOR}")
				add_definitions("-Wno-implicit-fallthrough")
			endif()
		endif()
	endif()

	if (!CMAKE_BUILD_TYPE STREQUAL "RELEASE")
		add_definitions("-g")
	endif()
endif()

set(lib_output_path ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${lib_output_path})

add_library(mediaserver_static STATIC ${lib_src})
SET_TARGET_PROPERTIES(mediaserver_static PROPERTIES OUTPUT_NAME "mediaserver")
link_directories(${lib_output_path})

if (${UNIX_OS})
	if (NOT ANDROID_STL MATCHES "gnustl_shared")
		set(CMAKE_SHARED_LINKER_FLAGS "-shared -O3 -flto")
	endif()
	target_compile_options(mediaserver_static PRIVATE -fvisibility=hidden)
endif()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/example)

##############################################################################
